﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Boxie
{
    public partial class frmDrawing : Form
    {
        public Bitmap bitDrawingPadRemote;
        private Bitmap bitDrawingPad;
        private Bitmap bitPixel;
        private Color colDrawColor = Color.Black;
        private float flDrawWidth = 1;
        private int lastX;
        private int lastY;

        public frmDrawing()
        {
            InitializeComponent();
            bitDrawingPad = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            bitDrawingPadRemote = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics.FromImage(bitDrawingPad).Clear(Color.White);
            bitPixel = new Bitmap(1, 1);
            if (BoxieRegLib.GetSettingBool("General/WindowTopMost", true))
                this.TopMost = true;
            tmrSend.Interval = BoxieRegLib.GetSettingInt("Drawing/UpdateInterval", 1000);
        }

        private void frmDrawing_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Point pt = new Point(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
            g.DrawImage(bitDrawingPad, pt);
        }

        private void frmDrawing_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Draw(lastX, lastY, e.X, e.Y, colDrawColor, flDrawWidth);
                lastX = e.X;
                lastY = e.Y;
            }
        }

        public void DrawingPadUpdate()
        {
            using (Graphics g = this.CreateGraphics())
            {
                g.DrawImage(bitDrawingPadRemote, new Point(0, 0));
            }
        }

        public void Draw(int lX, int lY, int X, int Y, Color c, float width)
        {
            Graphics g = Graphics.FromImage(bitDrawingPad);
            g.DrawLine(new Pen(c, width), lX, lY, X, Y);
            g.Dispose();
            g = this.CreateGraphics();
            g.DrawLine(new Pen(c, width), lX, lY, X, Y);
            g.Dispose();
        }

        private void frmDrawing_MouseDown(object sender, MouseEventArgs e)
        {
            lastX = e.X;
            lastY = e.Y;
        }

        private void blackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colDrawColor = Color.Black;
            blackToolStripMenuItem.Checked = true;
            redToolStripMenuItem.Checked = false;
            greenToolStripMenuItem.Checked = false;
            blueToolStripMenuItem.Checked = false;
            customToolStripMenuItem.Checked = false;
        }

        private void redToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colDrawColor = Color.Red;
            blackToolStripMenuItem.Checked = false;
            redToolStripMenuItem.Checked = true;
            greenToolStripMenuItem.Checked = false;
            blueToolStripMenuItem.Checked = false;
            customToolStripMenuItem.Checked = false;
        }

        private void greenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colDrawColor = Color.Green;
            blackToolStripMenuItem.Checked = false;
            redToolStripMenuItem.Checked = false;
            greenToolStripMenuItem.Checked = true;
            blueToolStripMenuItem.Checked = false;
            customToolStripMenuItem.Checked = false;
        }

        private void blueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colDrawColor = Color.Blue;
            blackToolStripMenuItem.Checked = false;
            redToolStripMenuItem.Checked = false;
            greenToolStripMenuItem.Checked = false;
            blueToolStripMenuItem.Checked = true;
            customToolStripMenuItem.Checked = false;
        }

        private void customToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colDrawColor = Color.Black;
            blackToolStripMenuItem.Checked = false;
            redToolStripMenuItem.Checked = false;
            greenToolStripMenuItem.Checked = false;
            blueToolStripMenuItem.Checked = false;
            customToolStripMenuItem.Checked = true;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            flDrawWidth = 1;
            toolStripMenuItem2.Checked = true;
            toolStripMenuItem3.Checked = false;
            toolStripMenuItem4.Checked = false;
            toolStripMenuItem5.Checked = false;
            toolStripMenuItem6.Checked = false;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            flDrawWidth = 2;
            toolStripMenuItem2.Checked = false;
            toolStripMenuItem3.Checked = true;
            toolStripMenuItem4.Checked = false;
            toolStripMenuItem5.Checked = false;
            toolStripMenuItem6.Checked = false;
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            flDrawWidth = 3;
            toolStripMenuItem2.Checked = false;
            toolStripMenuItem3.Checked = false;
            toolStripMenuItem4.Checked = true;
            toolStripMenuItem5.Checked = false;
            toolStripMenuItem6.Checked = false;
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            flDrawWidth = 4;
            toolStripMenuItem2.Checked = false;
            toolStripMenuItem3.Checked = false;
            toolStripMenuItem4.Checked = false;
            toolStripMenuItem5.Checked = true;
            toolStripMenuItem6.Checked = false;
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            flDrawWidth = 5;
            toolStripMenuItem2.Checked = false;
            toolStripMenuItem3.Checked = false;
            toolStripMenuItem4.Checked = false;
            toolStripMenuItem5.Checked = false;
            toolStripMenuItem6.Checked = true;
        }

        private void clearEverythingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ClearPad();
        }

        public void ClearPad()
        {
            Graphics g = Graphics.FromImage(bitDrawingPad);
            g.Clear(Color.White);
            g.Dispose();
            g = this.CreateGraphics();
            g.Clear(Color.White);
            g.Dispose();
        }

        private void frmDrawing_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmBoxie.instance.fL.add("Drawing session ended.", 2);
            if (BoxieWebLib.socket != null)
                BoxieWebLib.SendToBoxie("DRAWCLOSE");
            frmBoxie.instance.fD = null;
        }

        private unsafe void tmrSend_Tick(object sender, EventArgs e)
        {
            byte[] header = System.Text.Encoding.ASCII.GetBytes("DRAWUPDT");
            byte[] footer = System.Text.Encoding.ASCII.GetBytes("EOS");
            MemoryStream stream = new MemoryStream();

            BitmapData bData = bitDrawingPad.LockBits(new Rectangle(new Point(), bitDrawingPad.Size), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            int byteCount = bData.Stride * bitDrawingPad.Height;
            byte[] image = new byte[byteCount];
            Marshal.Copy(bData.Scan0, image, 0, byteCount);
            bitDrawingPad.UnlockBits(bData);

            stream.Write(header, 0, header.Length);
            stream.Write(image, 0, image.Length);
            stream.Write(footer, 0, footer.Length);

            BoxieWebLib.SendBytesToBoxie(stream.ToArray());
        }
    }
}
